/**
 * 常用工具方法
 */

// 延迟函数
export function delay(fn, delay = 300) {
	setTimeout(fn, delay)
}


/**
 * 当一个方法返回false时，多次执行它，以获得正确信息
 * @return {Promise}
 */
export function loopObtain(fn, maxLoopNumber = 10) {
	return async function(...args) {
		let result, number = 0
		while (number <= maxLoopNumber && !result) {
			result = await fn(...args)
			number++
		}
		return result
	}
}
// 成功的提示
export function okShow(msg) {
	uni.showToast({
		title: `${msg || '操作成功'}`,
		duration: 2000
	});
}
// 失败的提示
export function failShow(msg) {
	uni.showToast({
		icon: 'error',
		title: `${msg || '操作失败'}`,
		duration: 2000
	});
}
// 分装页面跳转
export function next(url, d) {
	let data = JSON.stringify(d)
	uni.navigateTo({
		url: `${url}?data=${data}`
	})
}

export function next1(url, d) {
	let data = JSON.stringify(d)
	uni.reLaunch({
		url: `${url}?data=${data}`
	})
}
export function ToastOk(msg) {
	return new Promise(resolve => {
		uni.showToast({
			title: msg,
			icon: 'success',
			duration: 3000
		})
	})
}

export function ToastFail(msg) {
	return new Promise(resolve => {
		uni.showToast({
			title: msg,
			icon: 'none',
			duration: 3000
		})
	})
}
export function downLoad(url) {
	wx.downloadFile({
		url: url,
		header: {
			Authorization: "Bearer " + uni.getStorageSync('personInfo').accessToken
		},
		success: (res) => {
			if (res.statusCode === 200) {
				var temp = res.tempFilePath
				wx.saveFile({
					tempFilePath: temp,
					success: (res) => {

						let saveFilePath = res.savedFilePath
						let suffix = utils.getFileSuffix(saveFilePath)
						wx.openDocument({
							filePath: saveFilePath,
							fileType: suffix,
							showMenu: true,
							success: (res) => {}
						})
					}
				})
			} else {
				wx.showModal({
					title: '提示:',
					content: '导出成功',
					showCancel: false,
					cancelText: '取消',
					cancelColor: '#000000',
					confirmText: '确定',
					confirmColor: '#3CC51F'
				})
			}
		},
		fail: () => {
			wx.showModal({
				title: '提示:',
				content: '导出失败',
				showCancel: false,
				cancelText: '取消',
				cancelColor: '#000000',
				confirmText: '确定',
				confirmColor: '#3CC51F'
			})
		}
	})
}


//导出
export function daoChu(path) {
	let that = this
	console.log(path, 'this.$baseUrl');
	uni.showLoading({
		title: that.$t('loading')
	});
	uni.downloadFile({
		url: path,
		success: (res) => {
			console.log("res: ", res);
			if (res.statusCode === 200) {
				var temp = res.tempFilePath
				uni.saveFile({
					tempFilePath: temp,
					success: (ret) => {
						var savedFilePath = ret.savedFilePath;
						setTimeout(() => {
							uni.openDocument({
								filePath: savedFilePath,
								fileType: 'pdf', //仅打开pdf格式的文件
								showMenu: true,
								success: function(file) {
									uni.hideLoading()
									console.log('打开文档成功');
								}
							});
						}, 1000)
					}
				})
			} else {
				uni.hideLoading()
				uni.showModal({
					title: '提示:',
					content: '打开成功',
					showCancel: false,
					cancelText: '取消',
					cancelColor: '#000000',
					confirmText: '确定',
					confirmColor: '#3CC51F'
				})
				uni.hideLoading()
			}
		},
		fail: () => {
			uni.hideLoading()
			uni.showModal({
				title: '提示:',
				content: '导出失败',
				showCancel: false,
				cancelText: '取消',
				cancelColor: '#000000',
				confirmText: '确定',
				confirmColor: '#3CC51F'
			})
			uni.hideLoading()
		}
	})

}

// 预览图片
export function imgShow(path) {
	// let path = this.$baseUrl + '@/static/ke_hu/tou_xiang.png'
	uni.previewImage({
		urls: [path]
	})
}

//保存图片到相册
export function savePhoto(path, tip) {
	let that = this
	uni.showLoading({
		title: that.$t('Saving')
	})
	uni.downloadFile({
		url: path,
		success: (res) => {
			if (res.statusCode === 200) {
				uni.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success: function() {
						uni.hideLoading()
						uni.showToast({
							title: that.$t(`${tip}`),
							icon: "none"
						});
					},
					fail: function() {
						uni.hideLoading()
						uni.showToast({
							title: that.$t('SavingError'),
							icon: "none"
						});
					}
				});
			}
		}
	})
}
// 金额用逗号分割，并只保留两位小数
export function formatNumberWithCommas(number) {
	return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function loading() {
	let that = this
	uni.showLoading({
		title: that.$t('Laoding'),
		
	})
}
export var newbaseUrl = 'https://app.myonewow.com'